package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.CommentLike;
import com.sxkf.template.entity.PostComment;
import com.sxkf.template.service.PostCommentService;
import com.sxkf.template.vo.response.PostCommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 帖子评论 前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-20 11:48:57
 */
@Api(tags = "动态（帖子）评论")
@RestController
@RequestMapping("/postComment")
public class PostCommentController {
    @Resource
    private PostCommentService postCommentService;

    @ApiOperation(value = "分页查询帖子评论")
    @GetMapping(value = "/page")
    public R<IPage<PostComment>> page(@RequestParam Integer pageNo,
                                      @RequestParam Integer pageSize,
                                      @RequestParam String postId) {
        Page<PostComment> result = postCommentService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<PostComment>()
                .eq(PostComment::getPostId, postId)
                .eq(PostComment::getPid, 0L));
        result.getRecords().forEach(item -> {
            item.setChildCount(postCommentService.count(new LambdaQueryWrapper<PostComment>()
                    .eq(PostComment::getTopId, item.getId())));
        });
        return R.success(result);
    }

    @ApiOperation(value = "查询某条评论的子评论")
    @GetMapping(value = "/listChild")
    public R<IPage<PostComment>> listChildComments(@RequestParam Integer pageNo,
                                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                                   @RequestParam String commentPid) {
        Page<PostComment> result = postCommentService.page(new Page<>(pageNo, pageSize)
                , new LambdaQueryWrapper<PostComment>().eq(PostComment::getTopId, commentPid).orderByDesc(PostComment::getCreateTime));
        result.getRecords().forEach(item->{
            PostComment comment = postCommentService.getById(item.getPid());
            if (comment!=null){
                item.setNote(item.getUserNickName()+"  回复  "+comment.getUserNickName());
            }
        });
        return R.success(result);
    }

    @ApiOperation(value = "查询某条评论")
    @GetMapping(value = "/oneComment")
    public R<PostComment> oneComment(@RequestParam String commentId) {
        PostComment postComment = postCommentService.getById(commentId);
        return R.success(postComment);
    }

    @ApiOperation(value = "某条评论点赞")
    @PostMapping(value = "/like")
    public R<String> likeById(@RequestBody CommentLike commentLike) {
        String res = postCommentService.likeById(commentLike);
        return R.success(res);
    }

    @ApiOperation(value = "某条评论是否点赞")
    @PostMapping(value = "/isLike")
    public R<Boolean> isLike(@RequestParam String userId,
                            @ApiParam("类型（1=帖子评论，2=商品评论）")
                            @RequestParam String type,
                            @RequestParam String commentId) {
        Boolean res = postCommentService.isLike(userId,type,commentId);
        return R.success(res);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping(value = "/add")
    public R<?> add(@RequestBody PostComment postComment) {
        postCommentService.add(postComment);
        return R.success();
    }

    @ApiOperation(value = "删除评论")
    @GetMapping(value = "/id")
    public R<?> delete(@RequestParam String id) {
        postCommentService.removeById(id);
        return R.success();
    }

    @ApiOperation("我的帖子评价")
    @GetMapping("/myComment")
    public R<IPage<PostCommentVo>> myComment(@RequestParam Integer pageNo,
                                             @RequestParam Integer pageSize,
                                             @RequestParam Long userId){

        Page<PostCommentVo> result = postCommentService.myComment(pageNo, pageSize, userId);

        return R.success(result);
    }



}
